package com.thssh.ui.lsn05.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.thssh.ui.R;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/15
 * email: zhangyugehu@163.com
 */

public class XfermodeCircleImageView extends View {


    private int mWidth;
    private int mHeight;

    private Paint mBitmapPaint;
    private Bitmap mBitmap;
    private BitmapShader mBitmapShader;
    private Matrix mBitmapMatrix;

    private Paint mCaverPaint;
    private Bitmap mCaverBitmap;

    public XfermodeCircleImageView(Context context) {
        this(context, null);
    }

    public XfermodeCircleImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XfermodeCircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCaverPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCaverPaint.setColor(Color.RED);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_5);
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mBitmapMatrix = new Matrix();
        mBitmapPaint.setShader(mBitmapShader);
        mWidth = mBitmap.getWidth();
        mHeight = mBitmap.getHeight();
    }

    /**
     * 创建一个占满空间的bitmap，并在中心画个内切圆
     */
    private void drawCaverBitmap() {
        mCaverBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mCaverBitmap);
        float cx = mWidth / 2;
        float cy = mHeight / 2;
        float radius = mWidth / 2;
        canvas.drawCircle(cx, cy, radius, mCaverPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        float scaleX = 1;
        float scaleY = 1;
        if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY){
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            scaleX = mWidth*1.0f / mBitmap.getWidth()*1.0f;
        }
        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY){
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
            scaleY = mHeight*1.0f / mBitmap.getHeight()*1.0f;
        }
        if(mWidth > mHeight){
            mWidth = mHeight;
            mBitmapMatrix.setScale(scaleY, scaleY);
        }else{
            mHeight = mWidth;
            mBitmapMatrix.setScale(scaleX, scaleX);
        }
        Log.d("XfermodeCircleImageView", "scaleY: " + scaleY + " x: " + scaleX);
        mBitmapShader.setLocalMatrix(mBitmapMatrix);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCaverBitmap();
        canvas.drawRect(0, 0, mWidth, mHeight, mBitmapPaint);
        mCaverPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mCaverPaint.setStyle(Paint.Style.STROKE);
        canvas.drawBitmap(mCaverBitmap, 0, 0, mCaverPaint);
        mCaverPaint.setXfermode(null);
    }
}
